package com.youan.log.modules.audit.dataimport;

import com.youan.log.common.bean.ValidateResult;
import com.youan.log.common.entity.ThreadTaskProgress;
import com.youan.log.common.threadtask.AbsTaskThread;
import com.youan.log.modules.audit.dataanalysis.AnalysisDataExtract;
import com.youan.log.modules.audit.dataimport.bean.ImportTaskKey;
import com.youan.log.modules.audit.dataimport.bean.TaskData;
import com.youan.log.modules.audit.entity.ImportRecord;
import com.youan.log.modules.audit.service.IImportRecordService;
import org.springblade.core.tool.utils.SpringUtil;

public class ImportDataThread extends AbsTaskThread {

    private Long importRecordId;

    private ImportRecord importRecord;
    private TaskData taskData;
    private IImportRecordService importRecordService;

    public ImportDataThread(Long importRecordId) {
        super(new ImportTaskKey());
        this.importRecordId = importRecordId;
        this.importRecordService = SpringUtil.getBean(IImportRecordService.class);
        this.importRecord = importRecordService.getById(importRecordId);
    }

    @Override
    protected void afterCheckFail(ThreadTaskProgress checkFailTask) {
        ImportRecord record = new ImportRecord();
        record.setId(this.importRecordId);
        record.setTaskStatus(checkFailTask.getTaskStatus());
        record.setTaskId(checkFailTask.getId());
        this.importRecordService.updateById(record);
    }

    @Override
    protected ValidateResult taskCheck() {
        PreImportValidate validate = new PreImportValidate();
        return validate.validate(importRecord.getDate());
    }

    @Override
    protected void afterCheckPass(Long taskId) {
        TaskDataGetter getter = new TaskDataGetter();
        TaskData taskData = getter.getTaskData(importRecord.getDate());
        this.taskData = taskData;

        ImportRecord record = new ImportRecord();
        record.setId(this.importRecordId);
        record.setDate(taskData.getDate());
        record.setTaskStatus(ImportTaskKey.THREAD_TASK_STATUS_START);
        record.setTaskId(taskId);
        record.setImportSFileSize(taskData.getSFile().getFileSize());
        record.setImportSLineNum(taskData.getSFile().getLineSize());
        record.setImportDFileSize(taskData.getDFile().getFileSize());
        record.setImportDLineNum(taskData.getDFile().getLineSize());
        this.importRecordService.updateById(record);
    }

    @Override
    protected void beforeDoTask() {
        super.taskProgress.setProgressCommitSize(10000);
    }

    @Override
    protected void doTask() {
        // 创建原始数据表
        TableCreator creator = new TableCreator(taskData.getDate());
        creator.create();

        // 将文件里的数据导入到原始数据表中
        DataImporter importer = new DataImporter(this.taskData, super.taskProgress);
        importer.importData();

        // 为原始数据包添加索引
        TableIndexCreator indexCreator = new TableIndexCreator(this.taskData.getTableName(), this.importRecordId);
        indexCreator.create();

        // 从原始数据表中执行查询并将结果保存到log_statistics表中
        DataAggregator aggregator = new DataAggregator(this.taskData, super.taskProgress, this.importRecordId);
        aggregator.aggregate();

        // 攻击行为分析
        AnalysisDataExtract extract = new AnalysisDataExtract(this.taskData, super.taskProgress, this.importRecordId);
        extract.extract();
    }

    @Override
    protected void afterSuccessEnd() {
        ImportRecord record = new ImportRecord();
        record.setId(this.importRecordId);
        record.setTaskStatus(ImportTaskKey.THREAD_TASK_STATUS_End);
        importRecordService.updateById(record);
    }

    @Override
    protected Integer getTaskProgressTotal() {
        long size = this.taskData.getSFile().getLineSize() + this.taskData.getDFile().getLineSize();
        return Math.toIntExact(size);
    }

    @Override
    protected void afterTaskFailueEnd() {
        ImportRecord record = new ImportRecord();
        record.setId(this.importRecordId);
        record.setTaskStatus(ImportTaskKey.THREAD_TASK_STATUS_ERROR);
        importRecordService.updateById(record);
    }
}
